home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group97a.txt / 000076_icon-group-sender _Sun Mar 9 09:10:12 1997.msg < prev    next >
Internet Message Format  |  2000-09-20  |  28KB

  1. Received: by cheltenham.cs.arizona.edu; Sun, 9 Mar 1997 09:30:28 MST
  2. Date: Sun, 9 Mar 1997 09:10:12 -0700 (MST)
  3. From: Dan Nicolaescu <done@ece.arizona.edu>
  4. X-Sender: done@florence
  5. To: icon-group@cs.arizona.edu
  6. Subject: icon.el updated
  7. Message-Id: <Pine.SOL.3.91.970308095151.913B-100000@florence>
  8. Mime-Version: 1.0
  9. Content-Type: TEXT/PLAIN; charset=US-ASCII
  10. Content-Length: 26545
  11. Errors-To: icon-group-errors@cs.arizona.edu
  12. Status: RO
  13.  
  14. Hi all!
  15.  
  16. I have tweaked a little the icon.el file to add support for syntax 
  17. coloring for icon files in emacs. 
  18. Use (setq font-lock-maximum-decoration t) to see all the effects.
  19.  
  20. I also added imenu support (do a M-x imenu-add-to-menubar and you will 
  21. get a menu containing the procedures in the icon file where when 
  22. selecting an entry the point is moved the the beginning of the 
  23. coresponding procedure).
  24.  
  25. I added hideshow support. This enables hiding the bodies of the 
  26. procedures (and when emacs-19.35 will be out also the comments) to ease 
  27. the navigation in an icon file. Use M-x hs-minor-mode and then 
  28. M-x hs-hide-all to see the effects.
  29.  
  30. About syntax coloring there are some limitations:
  31. -curently the "end" "global" "initial" "local" "link" "procedure" 
  32. "$include" "$define" keywords are showed in the same color as 
  33. the types. The next version of font-lock.el (it was posted in 
  34. gnu.emacs.sources in november-december I think) will have a new face 
  35. font-lock-builtin-face which will be used for this. 
  36. -the only the first variable from a local/global declaration will be 
  37. colored using font-lock-variable-face. it's not very likely to that I am 
  38. going to fix this, so if somebody does it, please let me know. If you 
  39. want to do this, take a look how this is done for c/c++.
  40. -am working on adding some more coloring in a $define statement.
  41.  
  42. Please take a look at the way I have grouped the keywords for coloring 
  43. and email me your ideas, comments and critics. 
  44.  
  45. Everything was only teste using emacs-19.34. I have no idea if it works 
  46. with any other versions.
  47.  
  48. Enjoy,
  49.     Dan
  50.  
  51. Here is the file:
  52.  
  53. ;;; icon.el --- mode for editing Icon code
  54.  
  55. ;; Copyright (C) 1989 Free Software Foundation, Inc.
  56.  
  57. ;; Author: Chris Smith <csmith@convex.com>
  58. ;; Created: 15 Feb 89
  59. ;; Keywords: languages
  60.  
  61. ;; This file is part of GNU Emacs.
  62.  
  63. ;; GNU Emacs is free software; you can redistribute it and/or modify
  64. ;; it under the terms of the GNU General Public License as published by
  65. ;; the Free Software Foundation; either version 2, or (at your option)
  66. ;; any later version.
  67.  
  68. ;; GNU Emacs is distributed in the hope that it will be useful,
  69. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  70. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  71. ;; GNU General Public License for more details.
  72.  
  73. ;; You should have received a copy of the GNU General Public License
  74. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  75. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  76. ;; Boston, MA 02111-1307, USA.
  77.  
  78. ;;; Commentary:
  79.  
  80. ;; A major mode for editing the Icon programming language.
  81.  
  82. ;;; Code:
  83.  
  84. (defvar icon-mode-abbrev-table nil
  85.   "Abbrev table in use in Icon-mode buffers.")
  86. (define-abbrev-table 'icon-mode-abbrev-table ())
  87.  
  88. (defvar icon-mode-map ()
  89.   "Keymap used in Icon mode.")
  90. (if icon-mode-map
  91.     ()
  92.   (let ((map (make-sparse-keymap "Icon")))
  93.     (setq icon-mode-map (make-sparse-keymap))
  94.     (define-key icon-mode-map "{" 'electric-icon-brace)
  95.     (define-key icon-mode-map "}" 'electric-icon-brace)
  96.     (define-key icon-mode-map "\e\C-h" 'mark-icon-function)
  97.     (define-key icon-mode-map "\e\C-a" 'beginning-of-icon-defun)
  98.     (define-key icon-mode-map "\e\C-e" 'end-of-icon-defun)
  99.     (define-key icon-mode-map "\e\C-q" 'indent-icon-exp)
  100.     (define-key icon-mode-map "\177" 'backward-delete-char-untabify)
  101.     (define-key icon-mode-map "\t" 'icon-indent-command)
  102.   
  103.     (define-key icon-mode-map [menu-bar] (make-sparse-keymap))
  104.     (define-key icon-mode-map [menu-bar icon]
  105.       (cons "Icon" map))
  106.     (define-key map [beginning-of-icon-defun] '("Beginning of function" . beginning-of-icon-defun))
  107.     (define-key map [end-of-icon-defun] '("End of function" . end-of-icon-defun))
  108.     (define-key map [comment-region] '("Comment Out Region" . comment-region))
  109.     (define-key map [indent-region] '("Indent Region" . indent-region))
  110.     (define-key map [indent-line] '("Indent Line" . icon-indent-command))
  111.     (put 'eval-region 'menu-enable 'mark-active)
  112.     (put 'comment-region 'menu-enable 'mark-active)
  113.     (put 'indent-region 'menu-enable 'mark-active)))
  114.  
  115.  
  116. (defvar icon-mode-syntax-table nil
  117.   "Syntax table in use in Icon-mode buffers.")
  118.  
  119. (if icon-mode-syntax-table
  120.     ()
  121.   (setq icon-mode-syntax-table (make-syntax-table))
  122.   (modify-syntax-entry ?\\ "\\" icon-mode-syntax-table)
  123.   (modify-syntax-entry ?# "<" icon-mode-syntax-table)
  124.   (modify-syntax-entry ?\n ">" icon-mode-syntax-table)
  125.   (modify-syntax-entry ?$ "." icon-mode-syntax-table)
  126.   (modify-syntax-entry ?/ "." icon-mode-syntax-table)
  127.   (modify-syntax-entry ?* "." icon-mode-syntax-table)
  128.   (modify-syntax-entry ?+ "." icon-mode-syntax-table)
  129.   (modify-syntax-entry ?- "." icon-mode-syntax-table)
  130.   (modify-syntax-entry ?= "." icon-mode-syntax-table)
  131.   (modify-syntax-entry ?% "." icon-mode-syntax-table)
  132.   (modify-syntax-entry ?< "." icon-mode-syntax-table)
  133.   (modify-syntax-entry ?> "." icon-mode-syntax-table)
  134.   (modify-syntax-entry ?& "." icon-mode-syntax-table)
  135.   (modify-syntax-entry ?| "." icon-mode-syntax-table)
  136.   (modify-syntax-entry ?\' "\"" icon-mode-syntax-table))
  137.  
  138. (defvar icon-indent-level 4
  139.   "*Indentation of Icon statements with respect to containing block.")
  140. (defvar icon-brace-imaginary-offset 0
  141.   "*Imagined indentation of a Icon open brace that actually follows a statement.")
  142. (defvar icon-brace-offset 0
  143.   "*Extra indentation for braces, compared with other text in same context.")
  144. (defvar icon-continued-statement-offset 4
  145.   "*Extra indent for lines not starting new statements.")
  146. (defvar icon-continued-brace-offset 0
  147.   "*Extra indent for substatements that start with open-braces.
  148. This is in addition to icon-continued-statement-offset.")
  149.  
  150. (defvar icon-auto-newline nil
  151.   "*Non-nil means automatically newline before and after braces
  152. inserted in Icon code.")
  153.  
  154. (defvar icon-tab-always-indent t
  155.   "*Non-nil means TAB in Icon mode should always reindent the current line,
  156. regardless of where in the line point is when the TAB command is used.")
  157.  
  158. (defvar icon-imenu-generic-expression
  159.       '((nil "^[ \t]*procedure[ \t]*\\(\\sw+\\)[ \t]*("  1))
  160.   "Imenu expression for Icon-mode.  See `imenu-generic-expression'.")
  161.  
  162.  
  163.  
  164. ;;;###autoload
  165. (defun icon-mode ()
  166.   "Major mode for editing Icon code.
  167. Expression and list commands understand all Icon brackets.
  168. Tab indents for Icon code.
  169. Paragraphs are separated by blank lines only.
  170. Delete converts tabs to spaces as it moves back.
  171. \\{icon-mode-map}
  172. Variables controlling indentation style:
  173.  icon-tab-always-indent
  174.     Non-nil means TAB in Icon mode should always reindent the current line,
  175.     regardless of where in the line point is when the TAB command is used.
  176.  icon-auto-newline
  177.     Non-nil means automatically newline before and after braces
  178.     inserted in Icon code.
  179.  icon-indent-level
  180.     Indentation of Icon statements within surrounding block.
  181.     The surrounding block's indentation is the indentation
  182.     of the line on which the open-brace appears.
  183.  icon-continued-statement-offset
  184.     Extra indentation given to a substatement, such as the
  185.     then-clause of an if or body of a while.
  186.  icon-continued-brace-offset
  187.     Extra indentation given to a brace that starts a substatement.
  188.     This is in addition to `icon-continued-statement-offset'.
  189.  icon-brace-offset
  190.     Extra indentation for line if it starts with an open brace.
  191.  icon-brace-imaginary-offset
  192.     An open brace following other text is treated as if it were
  193.     this far to the right of the start of its line.
  194.  
  195. Turning on Icon mode calls the value of the variable `icon-mode-hook'
  196. with no args, if that value is non-nil."
  197.   (interactive)
  198.   (kill-all-local-variables)
  199.   (use-local-map icon-mode-map)
  200.   (setq major-mode 'icon-mode)
  201.   (setq mode-name "Icon")
  202.   (setq local-abbrev-table icon-mode-abbrev-table)
  203.   (set-syntax-table icon-mode-syntax-table)
  204.   (make-local-variable 'paragraph-start)
  205.   (setq paragraph-start (concat "$\\|" page-delimiter))
  206.   (make-local-variable 'paragraph-separate)
  207.   (setq paragraph-separate paragraph-start)
  208.   (make-local-variable 'indent-line-function)
  209.   (setq indent-line-function 'icon-indent-line)
  210.   (make-local-variable 'require-final-newline)
  211.   (setq require-final-newline t)
  212.   (make-local-variable 'comment-start)
  213.   (setq comment-start "# ")
  214.   (make-local-variable 'comment-end)
  215.   (setq comment-end "")
  216.   (make-local-variable 'comment-column)
  217.   (setq comment-column 32)
  218.   (make-local-variable 'comment-start-skip)
  219.   (setq comment-start-skip "# *")
  220.   (make-local-variable 'comment-indent-function)
  221.   (setq comment-indent-function 'icon-comment-indent)
  222.   (make-local-variable 'font-lock-defaults)
  223.   (setq font-lock-defaults  
  224.     '((
  225.        icon-font-lock-keywords
  226.        icon-font-lock-keywords-1 
  227.        icon-font-lock-keywords-2
  228.        )
  229.       nil nil ((?_ . "w")) beginning-of-defun
  230.       (font-lock-comment-start-regexp . "#")
  231.       (font-lock-mark-block-function . mark-defun)))
  232.   (make-local-variable 'imenu-generic-expression)
  233.   (setq imenu-generic-expression icon-imenu-generic-expression)
  234.   (pushnew '(icon-mode  "procedure" "end" icon-forward-sexp-function)
  235.        hs-special-modes-alist :test 'equal)
  236. ;  (setq hs-block-start-regexp "procedure")
  237. ;  (setq hs-block-end-regexp "^end")
  238. ;  (setq hs-forward-sexp-func 'icon-forward-sexp-function)
  239.   (run-hooks 'icon-mode-hook))
  240.  
  241. ;; This is used by indent-for-comment to decide how much to
  242. ;; indent a comment in Icon code based on its context.
  243. (defun icon-comment-indent ()
  244.   (if (looking-at "^#")
  245.       0    
  246.     (save-excursion
  247.       (skip-chars-backward " \t")
  248.       (max (if (bolp) 0 (1+ (current-column)))
  249.        comment-column))))
  250.  
  251. (defun electric-icon-brace (arg)
  252.   "Insert character and correct line's indentation."
  253.   (interactive "P")
  254.   (let (insertpos)
  255.     (if (and (not arg)
  256.          (eolp)
  257.          (or (save-excursion
  258.            (skip-chars-backward " \t")
  259.            (bolp))
  260.          (if icon-auto-newline
  261.              (progn (icon-indent-line) (newline) t)
  262.            nil)))
  263.     (progn
  264.       (insert last-command-char)
  265.       (icon-indent-line)
  266.       (if icon-auto-newline
  267.           (progn
  268.         (newline)
  269.         ;; (newline) may have done auto-fill
  270.         (setq insertpos (- (point) 2))
  271.         (icon-indent-line)))
  272.       (save-excursion
  273.         (if insertpos (goto-char (1+ insertpos)))
  274.         (delete-char -1))))
  275.     (if insertpos
  276.     (save-excursion
  277.       (goto-char insertpos)
  278.       (self-insert-command (prefix-numeric-value arg)))
  279.       (self-insert-command (prefix-numeric-value arg)))))
  280.  
  281. (defun icon-indent-command (&optional whole-exp)
  282.   (interactive "P")
  283.   "Indent current line as Icon code, or in some cases insert a tab character.
  284. If `icon-tab-always-indent' is non-nil (the default), always indent current
  285. line.  Otherwise, indent the current line only if point is at the left margin
  286. or in the line's indentation; otherwise insert a tab.
  287.  
  288. A numeric argument, regardless of its value, means indent rigidly all the
  289. lines of the expression starting after point so that this line becomes
  290. properly indented.  The relative indentation among the lines of the
  291. expression are preserved."
  292.   (if whole-exp
  293.       ;; If arg, always indent this line as Icon
  294.       ;; and shift remaining lines of expression the same amount.
  295.       (let ((shift-amt (icon-indent-line))
  296.         beg end)
  297.     (save-excursion
  298.       (if icon-tab-always-indent
  299.           (beginning-of-line))
  300.       (setq beg (point))
  301.       (forward-sexp 1)
  302.       (setq end (point))
  303.       (goto-char beg)
  304.       (forward-line 1)
  305.       (setq beg (point)))
  306.     (if (> end beg)
  307.         (indent-code-rigidly beg end shift-amt "#")))
  308.     (if (and (not icon-tab-always-indent)
  309.          (save-excursion
  310.            (skip-chars-backward " \t")
  311.            (not (bolp))))
  312.     (insert-tab)
  313.       (icon-indent-line))))
  314.  
  315. (defun icon-indent-line ()
  316.   "Indent current line as Icon code.
  317. Return the amount the indentation changed by."
  318.   (let ((indent (calculate-icon-indent nil))
  319.     beg shift-amt
  320.     (case-fold-search nil)
  321.     (pos (- (point-max) (point))))
  322.     (beginning-of-line)
  323.     (setq beg (point))
  324.     (cond ((eq indent nil)
  325.        (setq indent (current-indentation)))
  326.       ((eq indent t)
  327.        (setq indent (calculate-icon-indent-within-comment)))
  328.       ((looking-at "[ \t]*#")
  329.        (setq indent 0))
  330.       (t
  331.        (skip-chars-forward " \t")
  332.        (if (listp indent) (setq indent (car indent)))
  333.        (cond ((and (looking-at "else\\b")
  334.                (not (looking-at "else\\s_")))
  335.           (setq indent (save-excursion
  336.                  (icon-backward-to-start-of-if)
  337.                  (current-indentation))))
  338.          ((or (= (following-char) ?})
  339.               (looking-at "end\\b"))
  340.           (setq indent (- indent icon-indent-level)))
  341.          ((= (following-char) ?{)
  342.           (setq indent (+ indent icon-brace-offset))))))
  343.     (skip-chars-forward " \t")
  344.     (setq shift-amt (- indent (current-column)))
  345.     (if (zerop shift-amt)
  346.     (if (> (- (point-max) pos) (point))
  347.         (goto-char (- (point-max) pos)))
  348.       (delete-region beg (point))
  349.       (indent-to indent)
  350.       ;; If initial point was within line's indentation,
  351.       ;; position after the indentation.  Else stay at same point in text.
  352.       (if (> (- (point-max) pos) (point))
  353.       (goto-char (- (point-max) pos))))
  354.     shift-amt))
  355.  
  356. (defun calculate-icon-indent (&optional parse-start)
  357.   "Return appropriate indentation for current line as Icon code.
  358. In usual case returns an integer: the column to indent to.
  359. Returns nil if line starts inside a string, t if in a comment."
  360.   (save-excursion
  361.     (beginning-of-line)
  362.     (let ((indent-point (point))
  363.       (case-fold-search nil)
  364.       state
  365.       containing-sexp
  366.       toplevel)
  367.       (if parse-start
  368.       (goto-char parse-start)
  369.     (setq toplevel (beginning-of-icon-defun)))
  370.       (while (< (point) indent-point)
  371.     (setq parse-start (point))
  372.     (setq state (parse-partial-sexp (point) indent-point 0))
  373.     (setq containing-sexp (car (cdr state))))
  374.       (cond ((or (nth 3 state) (nth 4 state))
  375.          ;; return nil or t if should not change this line
  376.          (nth 4 state))
  377.         ((and containing-sexp
  378.           (/= (char-after containing-sexp) ?{))
  379.          ;; line is expression, not statement:
  380.          ;; indent to just after the surrounding open.
  381.          (goto-char (1+ containing-sexp))
  382.          (current-column))
  383.         (t
  384.           (if toplevel
  385.           ;; Outside any procedures.
  386.           (progn (icon-backward-to-noncomment (point-min))
  387.              (if (icon-is-continuation-line)
  388.                  icon-continued-statement-offset 0))
  389.         ;; Statement level.
  390.         (if (null containing-sexp)
  391.             (progn (beginning-of-icon-defun)
  392.                (setq containing-sexp (point))))
  393.         (goto-char indent-point)
  394.         ;; Is it a continuation or a new statement?
  395.         ;; Find previous non-comment character.
  396.         (icon-backward-to-noncomment containing-sexp)
  397.         ;; Now we get the answer.
  398.         (if (icon-is-continuation-line)
  399.             ;; This line is continuation of preceding line's statement;
  400.             ;; indent  icon-continued-statement-offset  more than the
  401.             ;; first line of the statement.
  402.             (progn
  403.               (icon-backward-to-start-of-continued-exp containing-sexp)
  404.               (+ icon-continued-statement-offset (current-column)
  405.              (if (save-excursion (goto-char indent-point)
  406.                          (skip-chars-forward " \t")
  407.                          (eq (following-char) ?{))
  408.                  icon-continued-brace-offset 0)))
  409.           ;; This line starts a new statement.
  410.           ;; Position following last unclosed open.
  411.           (goto-char containing-sexp)
  412.           ;; Is line first statement after an open-brace?
  413.           (or
  414.             ;; If no, find that first statement and indent like it.
  415.             (save-excursion
  416.               (if (looking-at "procedure\\s ")
  417.               (forward-sexp 3)
  418.             (forward-char 1))
  419.               (while (progn (skip-chars-forward " \t\n")
  420.                     (looking-at "#"))
  421.             ;; Skip over comments following openbrace.
  422.             (forward-line 1))
  423.               ;; The first following code counts
  424.               ;; if it is before the line we want to indent.
  425.               (and (< (point) indent-point)
  426.                (current-column)))
  427.             ;; If no previous statement,
  428.             ;; indent it relative to line brace is on.
  429.             ;; For open brace in column zero, don't let statement
  430.             ;; start there too.  If icon-indent-level is zero,
  431.             ;; use icon-brace-offset + icon-continued-statement-offset
  432.             ;; instead.
  433.             ;; For open-braces not the first thing in a line,
  434.             ;; add in icon-brace-imaginary-offset.
  435.             (+ (if (and (bolp) (zerop icon-indent-level))
  436.                (+ icon-brace-offset
  437.                   icon-continued-statement-offset)
  438.              icon-indent-level)
  439.                ;; Move back over whitespace before the openbrace.
  440.                ;; If openbrace is not first nonwhite thing on the line,
  441.                ;; add the icon-brace-imaginary-offset.
  442.                (progn (skip-chars-backward " \t")
  443.                   (if (bolp) 0 icon-brace-imaginary-offset))
  444.                ;; Get initial indentation of the line we are on.
  445.                (current-indentation))))))))))
  446.  
  447. ;; List of words to check for as the last thing on a line.
  448. ;; If cdr is t, next line is a continuation of the same statement,
  449. ;; if cdr is nil, next line starts a new (possibly indented) statement.
  450.  
  451. (defconst icon-resword-alist
  452.   '(("by" . t) ("case" . t) ("create") ("do") ("dynamic" . t) ("else")
  453.     ("every" . t) ("if" . t) ("global" . t) ("initial" . t)
  454.     ("link" . t) ("local" . t) ("of") ("record" . t) ("repeat" . t)
  455.     ("static" . t) ("then") ("to" . t) ("until" . t) ("while" . t)))
  456.  
  457. (defun icon-is-continuation-line ()
  458.   (let* ((ch (preceding-char))
  459.      (ch-syntax (char-syntax ch)))
  460.     (if (eq ch-syntax ?w)
  461.     (assoc (buffer-substring
  462.         (progn (forward-word -1) (point))
  463.         (progn (forward-word 1) (point)))
  464.            icon-resword-alist)
  465.       (not (memq ch '(0 ?\; ?\} ?\{ ?\) ?\] ?\" ?\' ?\n))))))
  466.  
  467. (defun icon-backward-to-noncomment (lim)
  468.   (let (opoint stop)
  469.     (while (not stop)
  470.       (skip-chars-backward " \t\n\f" lim)
  471.       (setq opoint (point))
  472.       (beginning-of-line)
  473.       (if (and (nth 5 (parse-partial-sexp (point) opoint))
  474.            (< lim (point)))
  475.       (search-backward "#")
  476.     (setq stop t)))))
  477.  
  478. (defun icon-backward-to-start-of-continued-exp (lim)
  479.   (if (memq (preceding-char) '(?\) ?\]))
  480.       (forward-sexp -1))
  481.   (beginning-of-line)
  482.   (skip-chars-forward " \t")
  483.   (cond
  484.    ((<= (point) lim) (goto-char (1+ lim)))
  485.    ((not (icon-is-continued-line)) 0)
  486.    ((and (eq (char-syntax (following-char)) ?w)
  487.      (cdr
  488.       (assoc (buffer-substring (point)
  489.                    (save-excursion (forward-word 1) (point)))
  490.          icon-resword-alist))) 0)
  491.    (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim))))
  492.  
  493. (defun icon-is-continued-line ()
  494.   (save-excursion
  495.     (end-of-line 0)
  496.     (icon-is-continuation-line)))
  497.  
  498. (defun icon-backward-to-start-of-if (&optional limit)
  499.   "Move to the start of the last \"unbalanced\" if."
  500.   (or limit (setq limit (save-excursion (beginning-of-icon-defun) (point))))
  501.   (let ((if-level 1)
  502.     (case-fold-search nil))
  503.     (while (not (zerop if-level))
  504.       (backward-sexp 1)
  505.       (cond ((looking-at "else\\b")
  506.          (setq if-level (1+ if-level)))
  507.         ((looking-at "if\\b")
  508.          (setq if-level (1- if-level)))
  509.         ((< (point) limit)
  510.          (setq if-level 0)
  511.          (goto-char limit))))))
  512.  
  513. (defun mark-icon-function ()
  514.   "Put mark at end of Icon function, point at beginning."
  515.   (interactive)
  516.   (push-mark (point))
  517.   (end-of-icon-defun)
  518.   (push-mark (point))
  519.   (beginning-of-line 0)
  520.   (beginning-of-icon-defun))
  521.  
  522. (defun beginning-of-icon-defun ()
  523.   "Go to the start of the enclosing procedure; return t if at top level."
  524.   (interactive)
  525.   (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move)
  526.       (looking-at "e")
  527.     t))
  528.  
  529. (defun end-of-icon-defun ()
  530.   (interactive)
  531.   (if (not (bobp)) (forward-char -1))
  532.   (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move)
  533.   (forward-word -1)
  534.   (forward-line 1))
  535.  
  536. (defun indent-icon-exp ()
  537.   "Indent each line of the Icon grouping following point."
  538.   (interactive)
  539.   (let ((indent-stack (list nil))
  540.     (contain-stack (list (point)))
  541.     (case-fold-search nil)
  542.     restart outer-loop-done inner-loop-done state ostate
  543.     this-indent last-sexp
  544.     at-else at-brace at-do
  545.     (opoint (point))
  546.     (next-depth 0))
  547.     (save-excursion
  548.       (forward-sexp 1))
  549.     (save-excursion
  550.       (setq outer-loop-done nil)
  551.       (while (and (not (eobp)) (not outer-loop-done))
  552.     (setq last-depth next-depth)
  553.     ;; Compute how depth changes over this line
  554.     ;; plus enough other lines to get to one that
  555.     ;; does not end inside a comment or string.
  556.     ;; Meanwhile, do appropriate indentation on comment lines.
  557.     (setq innerloop-done nil)
  558.     (while (and (not innerloop-done)
  559.             (not (and (eobp) (setq outer-loop-done t))))
  560.       (setq ostate state)
  561.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  562.                       nil nil state))
  563.       (setq next-depth (car state))
  564.       (if (and (car (cdr (cdr state)))
  565.            (>= (car (cdr (cdr state))) 0))
  566.           (setq last-sexp (car (cdr (cdr state)))))
  567.       (if (or (nth 4 ostate))
  568.           (icon-indent-line))
  569.       (if (or (nth 3 state))
  570.           (forward-line 1)
  571.         (setq innerloop-done t)))
  572.     (if (<= next-depth 0)
  573.         (setq outer-loop-done t))
  574.     (if outer-loop-done
  575.         nil
  576.       (if (/= last-depth next-depth)
  577.           (setq last-sexp nil))
  578.       (while (> last-depth next-depth)
  579.         (setq indent-stack (cdr indent-stack)
  580.           contain-stack (cdr contain-stack)
  581.           last-depth (1- last-depth)))
  582.       (while (< last-depth next-depth)
  583.         (setq indent-stack (cons nil indent-stack)
  584.           contain-stack (cons nil contain-stack)
  585.           last-depth (1+ last-depth)))
  586.       (if (null (car contain-stack))
  587.           (setcar contain-stack (or (car (cdr state))
  588.                     (save-excursion (forward-sexp -1)
  589.                             (point)))))
  590.       (forward-line 1)
  591.       (skip-chars-forward " \t")
  592.       (if (eolp)
  593.           nil
  594.         (if (and (car indent-stack)
  595.              (>= (car indent-stack) 0))
  596.         ;; Line is on an existing nesting level.
  597.         ;; Lines inside parens are handled specially.
  598.         (if (/= (char-after (car contain-stack)) ?{)
  599.             (setq this-indent (car indent-stack))
  600.           ;; Line is at statement level.
  601.           ;; Is it a new statement?  Is it an else?
  602.           ;; Find last non-comment character before this line
  603.           (save-excursion
  604.             (setq at-else (looking-at "else\\W"))
  605.             (setq at-brace (= (following-char) ?{))
  606.             (icon-backward-to-noncomment opoint)
  607.             (if (icon-is-continuation-line)
  608.             ;; Preceding line did not end in comma or semi;
  609.             ;; indent this line  icon-continued-statement-offset
  610.             ;; more than previous.
  611.             (progn
  612.               (icon-backward-to-start-of-continued-exp (car contain-stack))
  613.               (setq this-indent
  614.                 (+ icon-continued-statement-offset (current-column)
  615.                    (if at-brace icon-continued-brace-offset 0))))
  616.               ;; Preceding line ended in comma or semi;
  617.               ;; use the standard indent for this level.
  618.               (if at-else
  619.               (progn (icon-backward-to-start-of-if opoint)
  620.                  (setq this-indent (current-indentation)))
  621.             (setq this-indent (car indent-stack))))))
  622.           ;; Just started a new nesting level.
  623.           ;; Compute the standard indent for this level.
  624.           (let ((val (calculate-icon-indent
  625.                (if (car indent-stack)
  626.                    (- (car indent-stack))))))
  627.         (setcar indent-stack
  628.             (setq this-indent val))))
  629.         ;; Adjust line indentation according to its contents
  630.         (if (or (= (following-char) ?})
  631.             (looking-at "end\\b"))
  632.         (setq this-indent (- this-indent icon-indent-level)))
  633.         (if (= (following-char) ?{)
  634.         (setq this-indent (+ this-indent icon-brace-offset)))
  635.         ;; Put chosen indentation into effect.
  636.         (or (= (current-column) this-indent)
  637.         (progn
  638.           (delete-region (point) (progn (beginning-of-line) (point)))
  639.           (indent-to this-indent)))
  640.         ;; Indent any comment following the text.
  641.         (or (looking-at comment-start-skip)
  642.         (if (re-search-forward comment-start-skip (save-excursion (end-of-line) (point)) t)
  643.             (progn (indent-for-comment) (beginning-of-line))))))))))
  644.  
  645. ;;"break" "do" "next" "repeat" "to" "by" "else" "if" "not" "return" "until" "case" "of" "static" "while" "create" "every" "suspend" "default" "fail" "record" "then" "?"
  646. (defconst icon-keywords "?\\|b\\(reak\\|y\\)\\|c\\(ase\\|reate\\)\\|d\\(efault\\|o\\)\\|e\\(lse\\|very\\)\\|fail\\|if\\|n\\(ext\\|ot\\)\\|of\\|re\\(cord\\|peat\\|turn\\)\\|s\\(tatic\\|uspend\\)\\|t\\(hen\\|o\\)\\|until\\|while")
  647.  
  648. ;; "end" "global" "initial" "local" "link" "procedure" "$include" "$define"
  649. (defconst icon-builtin "$\\(define\\|include\\)\\|end\\|global\\|initial\\|l\\(ink\\|ocal\\)\\|procedure")
  650.  
  651. ;;"null" "string" "co-expression" "table" "integer" "cset"  "set" "real" "file" "list"
  652. (defconst icon-type-types "c\\(o-expression\\|set\\)\\|file\\|integer\\|list\\|null\\|real\\|s\\(et\\|tring\\)\\|table")
  653.  
  654. ;;"&allocated" "&ascii" "&clock" "&col" "&collections" "&column" "&control" "&cset" "¤t" "&date" "&dateline" "&digits" "&dump" "&error" "&errornumber" "&errortext" "&errorvalue" "&errout" "&eventcode" "&eventsource" "&eventvalue" "&fail" "&features"
  655.  "&file" "&host" "&input" "&interval" "&lcase" "&ldrag" "&letters" "&level" "&line" "&lpress" "&lrelease" "&main" "&mdrag" "&meta" "&mpress" "&mrelease" "&null" "&output" "&phi" "&pi" "&pos" "&progname" "&random" "&rdrag" "®ions" "&resize" "&row" "&rpr
  656. ess" "&rrelease" "&shift" "&source" "&storage" "&subject" "&time" "&trace" "&ucase" "&version" "&window" "&x" "&y"
  657. (defconst icon-functions "&\\(a\\(llocated\\|scii\\)\\|c\\(lock\\|o\\(l\\(\\|lections\\|umn\\)\\|ntrol\\)\\|set\\|urrent\\)\\|d\\(ate\\(\\|line\\)\\|igits\\|ump\\)\\|e\\(rro\\(r\\(\\|number\\|text\\|value\\)\\|ut\\)\\|vent\\(code\\|source\\|value\\)\\)\\|
  658. f\\(ail\\|eatures\\|ile\\)\\|host\\|in\\(put\\|terval\\)\\|l\\(case\\|drag\\|e\\(tters\\|vel\\)\\|ine\\|press\\|release\\)\\|m\\(ain\\|drag\\|eta\\|press\\|release\\)\\|null\\|output\\|p\\(hi\\|i\\|os\\|rogname\\)\\|r\\(andom\\|drag\\|e\\(gions\\|size\\)\
  659. \|ow\\|press\\|release\\)\\|s\\(hift\\|ource\\|torage\\|ubject\\)\\|t\\(ime\\|race\\)\\|ucase\\|version\\|window\\|[exy]\\)")
  660.  
  661. (defvar icon-font-lock-keywords-1 nil)
  662. (defvar icon-font-lock-keywords icon-font-lock-keywords-1)
  663.  
  664. (setq icon-font-lock-keywords-1
  665.   (list
  666.    ;; Fontify function name definitions.
  667.    (list (concat "^[ \t]*procedure[ \t]*\\(\\sw+\\)[ \t]*(") 
  668.      1 'font-lock-function-name-face)
  669.    ))
  670.  
  671. (defvar icon-font-lock-keywords-2 nil)
  672. (setq icon-font-lock-keywords-2
  673.   (append icon-font-lock-keywords-1
  674.    (list
  675.     ;; Fontify all type specifiers.
  676.     (cons (concat "\\<\\(" icon-type-types "\\)\\>") 'font-lock-type-face)
  677.     ;;
  678.     ;; Fontify all builtin keywords.
  679.     (cons (concat "\\(" icon-keywords "\\)") 'font-lock-keyword-face)
  680.     (cons (concat "\\(" icon-builtin "\\)") 'font-lock-type-face)
  681.     (cons (concat "\\(" icon-functions "\\)") 'font-lock-reference-face)
  682.  
  683.     (list "^[ \t]*\\(global\\|local\\)[ \t]+\\(\\sw+\\)+\\([ \t]*,[ \t]*\\(\\sw+\\)\\)"  2 'font-lock-variable-name-face)
  684.     )))
  685.  
  686. (defun icon-forward-sexp-function (arg)
  687.   (if (> arg 0)
  688.       (re-search-forward "^[ \t]*end")
  689.     (re-search-backward "^[ \t]procedure")))
  690.  
  691.  
  692. ;;; icon.el ends here
  693.  
  694.